Two methods to access an object in a report
or subreport

Referencing objects in a report

Before you work with a report object, you have to reference it (open it). You can reference a report object either explicitly or implicitly:

Explicit reference - an object in the report

Assume that in your report you have a field with the Name property "OrderID". Since you know the field name, you can use code similar to this to reference the OrderID field directly.

Dim CRXReport As New CrystalReport1
Dim CRXReportField As FieldObject
Set CRXReportField = CRXReport.OrderID

Note:    Now that you have the OrderID field, you can put your code here to work with the properties of that field.

Implicit reference - cycling through the sections

If you are planning to work with a number of different kinds of report objects, you can cycle through the sections, test each of the report objects for kind, and manipulate them as you wish. You can do that using code similar to this:

    Dim CRXReport As New CrystalReport1 
    Dim CRXTables As CRAXDRT.DatabaseTables 
    Dim CRXTable As CRAXDRT.DatabaseTable 
    Dim CRXSections As CRAXDRT.Sections 
    Dim CRXSection As CRAXDRT.Section 
    Dim CRXSubreportObj As CRAXDRT.SubreportObject 
    Dim CRXReportObjects As CRAXDRT.ReportObjects 
    Dim CRXReportObject As Object 

Note:    You put this code in the Form_Load event of the startup form which contains the Report Viewer.

Start by getting the sections from the Main report.

Set CRXSections = CRXReport.Sections 

You begin cycling through each section in the main report.

For Each CRXSection In CRXSections 

In each section, you get all the objects in the section.

Set CRXReportObjects = CRXSection.ReportObjects 

You cycle through the objects.

For Each CRXReportObject In CRXReportObjects 

You test the objects to see if they're subreports.

If CRXReportObject.Kind = crSubreportObject Then 

When you find a subreport, you get a hold of it.

Set CRXSubreportObj = CRXReportObject 

Before you work with an object in a report, you have to reference it (open it).

Implicit reference - through a collection

If you are planning to work with a specific kind of report object, you can access the object by referencing the collection that contains that object. You reference a collection in a report through the appropriate Report property. Those properties, and the collections they get, are as follow:

Property Gets

Areas

Areas collection (Areas)

Database.DatabaseTables

Database Tables collection

FormulaFields

Formula Fields collection (FormulaFieldDefinitions)

GroupNameFields

Group Name Fields collection (GroupNameFieldDefinitions)

RunningTotalFields

Running Total Fields collection (RunningTotalFieldDefinitions)

GroupSortFields

Group Sort Fields collection (SortFields)

ParameterFields

Parameter Fields collection (ParameterFieldDefinitions)

RecordSortFields

Record Sort Fields collection (SortFields)

Sections

Section collection (Sections)

SQLExpressionFields

SQL Expression Fields collection (SQLExpressionFieldDefinitions)

SummaryFields

Summary Fields collection (SummaryFieldDefinitions)

Once you have the collection, you can cycle through the collection to locate the appropriate object you want. The following example shows you how to access the FormulaFieldDefinitions collection:

You can put this code in the General Declarations.

Dim CRXReport As New CrystalReport1 
Dim CRXFormulaField As CRAXDRT.FormulaFieldDefinition 

You can put this code in the Form_Load event procedure.

Private Sub Form_Load() 

This code gets the collection.

Set CRXFormulaFields = CRXReport.FormulaFields 
'You can put your code here to cycle through the collection to get the 
specific object you want

Finally you can view the report.

Crviewer1.ReportSource = CRXReport 
Crviewer1.ViewReport 
End Sub

Referencing objects in a subreport

Before you work with a subreport, you have to reference it (open it). Similar to referencing objects in a report, you can do this either explicitly or implicitly.

Explicit reference - the subreport itself

Assume that in your report you have a subreport with the Name property "Orders". Since you know the subreport name, you can use code similar to this to reference that subreport directly:

Dim CRXReport As New CrystalReport1
Dim CRXSubReport As SubreportObject
Set CRXSubReport = CRXReport.Orders

Note:    Now that you have the subreport, you can add code to manipulate the subreport.

Explicit reference - an object in the subreport

Assume that in your report you have a subreport with the Name property "Orders" and that, in that subreport, you have a field with the Name property "OrderID". Since you know the subreport and field names, you can use code similar to this to reference the OrderID field directly:

Dim CRXReport As New CrystalReport1
Dim CRXSubReport As SubreportObject
Set CRXSubReport = CRXReport.Subreport1
Dim CRXSubReportField As FieldObject
Set CRXSubReportField = CRXReport.Orders_OrderID

Now that you have the OrderID field, you can add code to work with the properties of that field.

Please note the special way you reference a field in a subreport by name. You must use:

The result, in this example, Orders_OrderID, appears to be a field in myReport with the name Orders_OrderID. It is actually a field in the Orders subreport; the program uses dynamic type info to generate these names at runtime. You can find the names by looking at the Project in the Object Browser.

Implicit reference

Though this approach is more complex, it is not bound to a particular report, and the code can be reused with minor modifications for other reports that contain subreports. You can use code similar to the following to access any and all subreport(s) in a particular report.

You can declare the Variables in the General Declarations section of a form.

Dim CRXReport As New CrystalReport1 
Dim CRXTables As CRAXDRT.DatabaseTables 
Dim CRXTable As CRAXDRT.DatabaseTable 
Dim CRXSections As CRAXDRT.Sections 
Dim CRXSection As CRAXDRT.Section 
Dim CRXSubreportObj As CRAXDRT.SubreportObject 
Dim CRXReportObjects As CRAXDRT.ReportObjects 
Dim CRXSubreport As CRAXDRT.Report 
Dim CRXReportObject As Object 

You put this code in the Form_Load event of the startup form which contains the Report Viewer.

You start by getting the sections from the Main report.

Set CRXSections = CRXReport.Sections 

You begin by cycling through each section in the main report.

For Each CRXSection In CRXSections 

In each section, you get all the objects in the section.

Set CRXReportObjects = CRXSection.ReportObjects 

You cycle through the objects.

For Each CRXReportObject In CRXReportObjects 

You test the objects to see if they're subreports.

If CRXReportObject.Kind = crSubreportObject Then 

When you find a subreport, you get a hold of it.

Set CRXSubreportObj = CRXReportObject 

Finally, you open the subreport and treat it as you would any other report.

Set CRXSubreport = CRXSubreportObj.OpenSubreport

Note:    This is where you put the code for manipulating the subreport.

End If 
Next CRXReportObject 
Next CRXSection 


Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com